package com.hz.friends.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.avos.avoscloud.AVUser;
import com.hz.friends.chat.Helper.CCPConfig;
import com.hz.friends.chat.Helper.CCPHelper;
import com.hz.friends.location.LocationService;
import com.hz.friends.utils.BitmapUtils;

public class WelcomeAcitivity extends BaseActivity {
	private LinearLayout layoutips;// 小圆点布局
	private ViewPager mviewpager;// viewpage
	private Bitmap greybitmap; // 灰色小圆点
	private Bitmap whitebitmap;// 白色小圆点
	private Button btn_register;
	private Button btn_login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		init();
		initViews();
		initEvents();
		setViews();
	}

	@Override
	protected void init() {
		AVUser user = AVUser.getCurrentUser();
		if (user != null) {// 已登录
		// CCPHelper.getInstance().registerCCP(null);
			// 赋值voip信息
			startActivity(MaintabsActivity.class);
			CCPHelper.getInstance().setCountInit(3);
			CCPHelper.getInstance().initVoipDevice();
			finish();
		}
	}

	@Override
	protected void initViews() {
		layoutips = (LinearLayout) findViewById(R.id.layouttips); // 小圆点布局
		mviewpager = (ViewPager) findViewById(R.id.mviewpager); // viewpage
		btn_register = (Button) findViewById(R.id.btn_register); // viewpage
		btn_login = (Button) findViewById(R.id.btn_login); // viewpage
		btn_login.getBackground().setAlpha(180);
		btn_register.getBackground().setAlpha(180);
	}

	@Override
	protected void setViews() {
		List<View> viewlists = new ArrayList<View>();
		ViewPager.LayoutParams lap = new ViewPager.LayoutParams();
		lap.width = ViewPager.LayoutParams.FILL_PARENT;
		lap.height = ViewPager.LayoutParams.FILL_PARENT;
		ImageView iv1 = new ImageView(this);
		ImageView iv2 = new ImageView(this);
		ImageView iv3 = new ImageView(this);
		// ImageView iv4 = new ImageView(this);

		iv1.setImageResource(R.drawable.test_bj_welcom1);
		iv2.setImageResource(R.drawable.test_bj_welcom2);
		iv3.setImageResource(R.drawable.test_bj_welcom3);
		// iv4.setImageResource(R.drawable.test_wel4);
		// iv1.setLayoutParams(lap);
		// iv2.setLayoutParams(lap);
		// iv3.setLayoutParams(lap);
		// iv4.setLayoutParams(lap);
		iv1.setScaleType(ScaleType.CENTER_CROP);
		iv2.setScaleType(ScaleType.CENTER_CROP);
		iv3.setScaleType(ScaleType.CENTER_CROP);
		// iv4.setScaleType(ScaleType.CENTER_CROP);
		viewlists.add(iv1);
		viewlists.add(iv2);
		viewlists.add(iv3);
		// viewlists.add(iv4);

		int widthmargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 3, getResources()
						.getDisplayMetrics());
		int width = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
						.getDisplayMetrics());
		whitebitmap = BitmapUtils.drawCycleImage("#ffffff", width);
		greybitmap = BitmapUtils.drawCycleImage("#8c8c8c", width);

		// 添加小圆点
		for (int i = 0; i < viewlists.size(); i++) {
			ImageView ivnode = new ImageView(this);
			LinearLayout.LayoutParams nodelap = new LinearLayout.LayoutParams(
					width, width);
			nodelap.leftMargin = widthmargin;
			nodelap.rightMargin = widthmargin;
			ivnode.setLayoutParams(nodelap);
			ivnode.setImageBitmap(greybitmap);
			if (i == 0) {
				ivnode.setImageBitmap(whitebitmap);
			}
			layoutips.addView(ivnode);
		}
		PagerAdapter mpageadpter = new MyViewPagerAdapter(viewlists);
		mviewpager.setAdapter(mpageadpter);
	}

	@Override
	protected void initEvents() {
		mviewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				selectTipsnode(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	// 小圆点选中
	private void selectTipsnode(int index) {
		for (int i = 0; i < layoutips.getChildCount(); i++) {
			ImageView ivnode = (ImageView) layoutips.getChildAt(i);
			ivnode.setImageBitmap(greybitmap);
			if (i == index)
				ivnode.setImageBitmap(whitebitmap);
		}
	}

	// 注册
	public void onRegister(View view) {
		startActivity(RegisterActivity.class);
		this.finish();
	}

	// 登录
	public void onLogin(View view) {
		startActivity(LoginActivity.class);
		this.finish();
	}

	// viewpageadpter
	public class MyViewPagerAdapter extends PagerAdapter {
		private List<View> mListViews;

		public MyViewPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mListViews.get(position));
		}

		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListViews.get(position), 0);
			return mListViews.get(position);
		}

		public int getCount() {
			return mListViews.size();
		}

		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

}
